<template>
  <div>
    <el-dialog
      :title="'新建属性'"
      v-model="isCreate"
      width="500px"
      :before-close="cancelCreate"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="formData"
        label-width="100px"
      >
        <el-form-item label="属性CODE" prop="propertyKey">
          <el-input
            type="text"
            placeholder="请输入属性CODE"
            v-model="formData.propertyKey"
            @input="setValue('propertyKey')"
            maxlength="10"
            :show-word-limit="true"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="属性名称" prop="name">
          <el-input
            type="text"
            placeholder="请输入属性名称"
            v-model="formData.name"
            maxlength="30"
            :show-word-limit="true"
          >
          </el-input>
        </el-form-item>

        <el-form-item label="设为系统属性" prop="classifyType">
          <el-switch
            v-model="formData.classifyType"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>

        <el-form-item label="类型" prop="propertyType">
          <el-select v-model="formData.propertyType" placeholder="请选择类型">
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancelCreate">取 消</el-button>
          <el-button type="primary" @click="formValidate">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import CustomerApi from "@/api/customerApi";
export default {
  props: {
    isCreate: {
      require: false,
      type: Boolean,
      default: () => {
        return false;
      },
    },
  },

  data() {
    return {
      formData: {
        name: "",
        propertyKey: "",
        classifyType: 0,
        propertyType: "",
        userGroupId: "",
      },
      typeList: [
        { id: 0, name: "数字" },
        { id: 1, name: "字符串" },
        { id: 2, name: "时间" },
        { id: 3, name: "日期" },
      ],
      rules: {
        propertyKey: [
          { required: true, message: "请输入属性CODE", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入属性名称", trigger: "blur" }],
        propertyType: [
          { required: true, message: "请选择类型", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.formData.userGroupId = this.$route.params.id;
  },
  methods: {
    formValidate() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.addProperty();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async addProperty() {
      let rs = await CustomerApi.addProperty(this.formData);
      if (rs.code == 200) {
        ElMessage.success("添加属性成功");
        this.confirmCreate();
      }
    },
    setValue(propertyKey) {
      this.formData[propertyKey] = this.formData[propertyKey].replace(
        /[^\w_]/g,
        ""
      );
    },
    confirmCreate() {
      this.$emit("confirmCreate");
      this.cancelCreate();
    },
    cancelCreate() {
      this.$emit("cancelCreate");
      this.$refs.formData.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped></style>
